package 合并k个已排序的链表;

import java.util.*;

class ListNode {
    int val;
    ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }
}

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param lists ListNode类ArrayList
     * @return ListNode类
     */
    static int N = 5010, cnt;
    static int[] arr = new int[N];

    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        PriorityQueue<ListNode> q = new PriorityQueue<>((v1, v2) -> {
            return v1.val - v2.val;
        });
        for (int i = 0; i < lists.size(); i++) {
            ListNode t = lists.get(i);
            if (t != null) {
                q.offer(t);
            }
        }
        ListNode ret = new ListNode(0);
        ListNode cur = ret;
        while (!q.isEmpty()) {
            ListNode tmp = q.poll();
            cur.next = tmp;
            cur = cur.next;
            if (tmp.next != null) {
                q.offer(tmp.next);
            }
        }
        return ret.next;
    }
}